package com.cskaoyan.service;

import com.cskaoyan.bean.pojo.Category;
import com.cskaoyan.bean.pojo.CategoryExample;
import com.cskaoyan.bean.vo.CataLogIndexVO;
import com.cskaoyan.bean.vo.CatalogCurrentVO;
import com.cskaoyan.bean.vo.CategoryListBean;
import com.cskaoyan.dao.CategoryMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

@SuppressWarnings("all")
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public CataLogIndexVO catalogIndex() {
        CategoryListBean currentCategoryBean = getCurrentCategoryBean();
        List<CategoryListBean> categoryList = getCategoryList();
        List<CategoryListBean> currentSubCategory = getCurrentSubCategory(currentCategoryBean);
        return new CataLogIndexVO(currentCategoryBean, categoryList, currentSubCategory);
    }

    @Override
    public CatalogCurrentVO catalogCurrent(Integer id) {
        CategoryListBean currentCategoryBean = getCatalogCurrent(id);
        List<CategoryListBean> currentSubCategory = getCatalogCurrentSub(currentCategoryBean);
        return new CatalogCurrentVO(currentCategoryBean, currentSubCategory);
    }

    private List<CategoryListBean> getCatalogCurrentSub(CategoryListBean currentCategoryBean) {
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andPidEqualTo(currentCategoryBean.getId());
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        List<CategoryListBean> currentSubCategory = new ArrayList<>();
        for (Category category : categories) {
            currentSubCategory.add(new CategoryListBean(category.getId(), category.getName(), category.getKeywords(),
                    category.getDesc(), category.getPid(), category.getIconUrl(), category.getPicUrl(),
                    category.getLevel(), (int) category.getSortOrder(), category.getAddTime(), category.getUpdateTime(),
                    category.getDeleted()));
        }

        return currentSubCategory;
    }

    private CategoryListBean getCatalogCurrent(int id) {
        CategoryExample categoryExample = new CategoryExample();
        Category category = categoryMapper.selectByPrimaryKey(id);
        return new CategoryListBean(category.getId(), category.getName(), category.getKeywords(),
                category.getDesc(), category.getPid(), category.getIconUrl(), category.getPicUrl(),
                category.getLevel(), (int) category.getSortOrder(), category.getAddTime(), category.getUpdateTime(),
                category.getDeleted());
    }

    private List<CategoryListBean> getCurrentSubCategory(CategoryListBean currentCategoryBean) {
        List<CategoryListBean> currentSubCategory = new ArrayList<>();
        currentSubCategory.add(new CategoryListBean(currentCategoryBean.getId(), currentCategoryBean.getName(), currentCategoryBean.getKeywords(),
                currentCategoryBean.getDesc(), currentCategoryBean.getPid(), currentCategoryBean.getIconUrl(), currentCategoryBean.getPicUrl(),
                currentCategoryBean.getLevel(), currentCategoryBean.getSortOrder(), currentCategoryBean.getAddTime(), currentCategoryBean.getUpdateTime(),
                currentCategoryBean.isDeleted()));
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andPidEqualTo(currentCategoryBean.getId());
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        for (Category category : categories) {
            currentSubCategory.add(new CategoryListBean(category.getId(), category.getName(), category.getKeywords(),
                    category.getDesc(), category.getPid(), category.getIconUrl(), category.getPicUrl(),
                    category.getLevel(), (int) category.getSortOrder(), category.getAddTime(), category.getUpdateTime(),
                    category.getDeleted()));
        }

        return currentSubCategory;
    }

    private List<CategoryListBean> getCategoryList() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andLevelEqualTo("L1");
        criteria.andDeletedEqualTo(false);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        List<CategoryListBean> categoryListBeans = new ArrayList<>();
        for (Category category : categories) {
            categoryListBeans.add(new CategoryListBean(category.getId(), category.getName(), category.getKeywords(),
                    category.getDesc(), category.getPid(), category.getIconUrl(), category.getPicUrl(),
                    category.getLevel(), (int) category.getSortOrder(), category.getAddTime(), category.getUpdateTime(),
                    category.getDeleted()));
        }
        return categoryListBeans;
    }

    private CategoryListBean getCurrentCategoryBean() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        PageHelper.startPage(1, 1);
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        Category category = categories.get(0);
        CategoryListBean currentCategoryBean = new CategoryListBean();
        currentCategoryBean.setId(category.getId());
        currentCategoryBean.setName(category.getName());
        currentCategoryBean.setKeywords(category.getKeywords());
        currentCategoryBean.setDesc(category.getDesc());
        currentCategoryBean.setPid(category.getPid());
        currentCategoryBean.setIconUrl(category.getIconUrl());
        currentCategoryBean.setPicUrl(category.getPicUrl());
        currentCategoryBean.setLevel(category.getLevel());
        currentCategoryBean.setSortOrder((int) category.getSortOrder());
        currentCategoryBean.setAddTime(category.getAddTime());
        currentCategoryBean.setDeleted(category.getDeleted());
        currentCategoryBean.setUpdateTime(category.getUpdateTime());
        return currentCategoryBean;
    }
}


